import { getDeviceParameterDb } from '../lib/device-parameter-database';

// 映射显示名称到实际表名
const displayNameToTableName: Record<string, string> = {
  '表 C.2': 'table_c2',
  '表C.2': 'table_c2',
  '表 C.4': 'table_c4',
  '表C.4': 'table_c4',
  '表 C.6': 'table_c6',
  '表C.6': 'table_c6',
  '表 C.14': 'table_c14',
  '表C.14': 'table_c14',
  '表 C.18': 'table_c18',
  '表C.18': 'table_c18',
  '表 C.27': 'table_c27',
  '表C.27': 'table_c27',
  '表 C.30': 'table_c30',
  '表C.30': 'table_c30',
  '表 C.36': 'table_c36',
  '表C.36': 'table_c36',
  '表 C.81': 'table_c81',
  '表C.81': 'table_c81',
  '表 C.109-1': 'table_c109_1',
  '表C.109-1': 'table_c109_1',
  '表 C.144': 'table_c144',
  '表C.144': 'table_c144',
  '表 C.150': 'table_c150',
  '表C.150': 'table_c150',
  '表 C.173': 'table_c173',
  '表C.173': 'table_c173',
  '表 C.174': 'table_c174',
  '表C.174': 'table_c174',
  '表 C.178': 'table_c178',
  '表C.178': 'table_c178',
  '表 C.179': 'table_c179',
  '表C.179': 'table_c179',
  '表 C.183': 'table_c183',
  '表C.183': 'table_c183',
  '表 C.201': 'table_c201',
  '表C.201': 'table_c201',
  '表 C.202': 'table_c202',
  '表C.202': 'table_c202',
  '表 C.203': 'table_c203',
  '表C.203': 'table_c203',
  '表 C.204': 'table_c204',
  '表C.204': 'table_c204',
  '表 C.207': 'table_c207',
  '表C.207': 'table_c207',
  '表 C.208': 'table_c208',
  '表C.208': 'table_c208',
  '表 C.212': 'table_c212',
  '表C.212': 'table_c212',
  '表 C.469': 'table_c469',
  '表C.469': 'table_c469',
  '表 C.470': 'table_c470',
  '表C.470': 'table_c470',
  '表 C.483': 'table_c483',
  '表C.483': 'table_c483',
  '表 C.485': 'table_c485',
  '表C.485': 'table_c485',
  '表 C.494': 'table_c494',
  '表C.494': 'table_c494',
  '表 C.496': 'table_c496',
  '表C.496': 'table_c496',
  '表 C.497': 'table_c497',
  '表C.497': 'table_c497'
};

async function fixDeviceMappingsFromOriginal() {
  try {
    const db = getDeviceParameterDb();
    const deviceParameterDb = db as any;

    console.log('开始基于原始设备数据修正设备类型映射...');

    // 获取所有设备的原始表名信息
    const devicesQuery = `
      SELECT DISTINCT 
        classification_code, 
        classification_name, 
        specialty,
        naming_table_id as original_naming,
        position_table_id as original_position,
        base_params_table_id as original_base_params
      FROM devices 
      WHERE classification_code IS NOT NULL
    `;

    const devices = deviceParameterDb.db.prepare(devicesQuery).all();
    console.log(`找到 ${devices.length} 个不同的设备分类`);

    // 更新映射表
    const updateMappingStmt = deviceParameterDb.db.prepare(`
      UPDATE device_type_mappings 
      SET 
        naming_table = ?,
        position_table = ?,
        base_params_table = ?,
        updated_at = datetime('now')
      WHERE classification_code = ?
    `);

    let updatedCount = 0;
    
    for (const device of devices) {
      // 将显示名称转换为实际表名
      const actualNamingTable = displayNameToTableName[device.original_naming] || device.original_naming;
      const actualPositionTable = displayNameToTableName[device.original_position] || device.original_position;
      const actualBaseParamsTable = displayNameToTableName[device.original_base_params] || device.original_base_params;
      
      try {
        updateMappingStmt.run(
          actualNamingTable,
          actualPositionTable,
          actualBaseParamsTable,
          device.classification_code
        );
        
        console.log(`修正映射: ${device.classification_code} - ${device.classification_name}`);
        console.log(`  命名表: ${device.original_naming} -> ${actualNamingTable}`);
        console.log(`  位置表: ${device.original_position} -> ${actualPositionTable}`);
        console.log(`  参数表: ${device.original_base_params} -> ${actualBaseParamsTable}`);
        console.log('');
        
        updatedCount++;
      } catch (error) {
        console.error(`修正映射失败 ${device.classification_code}:`, error);
      }
    }

    console.log(`\n成功修正了 ${updatedCount} 个设备类型映射`);
    console.log('映射修正完成！');

  } catch (error) {
    console.error('修正设备映射失败:', error);
    process.exit(1);
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  fixDeviceMappingsFromOriginal();
}